<?php
/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2018/5/3
 * Time: 上午9:48
 */

namespace common\service;

use common\helpers\predis\Predis;
use common\models\CouponCategory;

/**
 * Class CouponCategoryService
 * @package common\service
 * @SuppressWarnings(PHPMD)
 */
class CouponCategoryService
{

    public function getList()
    {
        $cacheKey = CouponCategoryService::getListCacheKey();
        $booleanExist = Predis::getInstance()->getClient()->client->exists($cacheKey);
        if ($booleanExist) {
            $cacheData = Predis::getInstance()->getClient()->client->get($cacheKey);
            return unserialize($cacheData);
        }

        $temp = [];
        $couponCategoryList = CouponCategory::find()->select(['id', 'title',  'status', 'type', 'mark_id',  'title', 'local_mark_id'])->orderBy("id desc")->all();
        $tempCouponCategoryLocalMarks = [];
        foreach ($couponCategoryList as $tempCouponCategory) {
            if (!in_array($tempCouponCategory['local_mark_id'], $tempCouponCategoryLocalMarks)) {
                $tempCouponCategoryLocalMarks[] = $tempCouponCategory['local_mark_id'];
            }
        }
        foreach ($couponCategoryList as $couponCategory) {
            foreach ($tempCouponCategoryLocalMarks as $tempCouponCategoryLocalMark) {
                if ($couponCategory['local_mark_id'] == $tempCouponCategoryLocalMark) {
                    $temp[$tempCouponCategoryLocalMark][] = $couponCategory;
                }
            }
        }

        if (!empty($temp)) {
            Predis::getInstance()->getClient()->client->set($cacheKey, serialize($temp));
        }
        return $temp;
    }

    public function getCategory($id)
    {
        $id = intval($id);
        $key = self::getCategoryCacheKey($id);
        $booleanExist = Predis::getInstance()->getClient()->client->exists($key);
        if (!$booleanExist) {
            $couponCategoryCollection = CouponCategory::getCollection();
            $category = $couponCategoryCollection->findOne(['id' => $id]);
            Predis::getInstance()->getClient()->client->hMset($key, $category);
        }
        $category = Predis::getInstance()->getClient()->client->hGetAll($key);
        return $category;
    }

    public function getCategoryById($_id)
    {
        $key = self::getCategoryCacheKey($_id);
        $booleanExist = Predis::getInstance()->getClient()->client->exists($key);
        if (!$booleanExist) {
            $couponCategoryCollection = CouponCategory::getCollection();
            $category = $couponCategoryCollection->findOne(['_id' => $_id]);
            Predis::getInstance()->getClient()->client->hMset($key, $category);
        }
        $category = Predis::getInstance()->getClient()->client->hGetAll($key);
        return $category;
    }


    public static function getListCacheKey()
    {
        return str_replace('\\', '-', __METHOD__);
    }

    public static function getCategoryCacheKey($id)
    {
        return str_replace('\\', '-', __METHOD__) . '-' . $id;
    }
}
